package junior.util.address;

import java.io.Serializable;

public class AddressToken implements Serializable{
	
	private static final long serialVersionUID = 1L;

	private final AddressTokenLevel level;
	
	private final String token;
	
	private final String primary;
	
	private final String suffix;
	
	private int routing;
	
	public AddressToken(AddressTokenLevel level, String token){
		String suffix = "";
		for(String _suffix: level.suffixs){
			if(token.endsWith(_suffix)){
				suffix = _suffix;
				break;
			}
		}
		this.level = level;
		this.token = token;
		this.suffix = suffix;
		this.primary = token.substring(0, token.length() - suffix.length());
	}
	
	AddressToken(AddressTokenLevel level, String token, String suffix, int routing){
		this.level = level;
		this.token = token;
		this.suffix = suffix;
		this.primary = token.substring(0, token.length() - suffix.length());
		this.routing = routing;
	}

	public AddressTokenLevel getLevel() {
		return level;
	}

	public String getToken() {
		return token;
	}
	
	public String getPrimary() {
		return primary;
	}

	public String getSuffix() {
		return suffix;
	}

	public int getRouting() {
		return routing;
	}

	public void setRouting(int routing) {
		this.routing = routing;
	}

	@Override
	public String toString() {
		return String.format("{\"level\":\"%s\", \"token\":\"%s\", \"primary\":\"%s\"}", level, token, primary);
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((level == null) ? 0 : level.hashCode());
		result = prime * result + ((primary == null) ? 0 : primary.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (!(obj instanceof AddressToken))
			return false;
		AddressToken other = (AddressToken) obj;
		if (level != other.level)
			return false;
		if (primary == null) {
			if (other.primary != null)
				return false;
		} else if (!primary.equals(other.primary))
			return false;
		return true;
	}

	
}
